package com.example.forum.controller;

import com.example.forum.common.AppResult;
import com.example.forum.common.ResultCode;
import com.example.forum.config.AppConfig;
import com.example.forum.model.Message;
import com.example.forum.model.User;
import com.example.forum.service.MessageService;
import com.example.forum.service.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import jdk.jfr.Frequency;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("/message")
@RestController
@Slf4j
public class MessageController {
    @Resource
    private MessageService messageService;
    @Resource
    private UserService userService;

//    发送
    @RequestMapping("/send")
    public AppResult send(HttpServletRequest request,
                          @RequestParam(value = "content")@NonNull String content,
                          @RequestParam(value = "receiveUserId")@NonNull Long receiveUserId){
        //获取当前登录的用户信息
        HttpSession session= request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        //1.当前登录的用户的状态，如是禁言则不能发站内信
        if (user.getState()==1){
            // 返回用户状态异常
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        //2.不能给自己发站内信
        if (user.getId() == receiveUserId){
            return AppResult.failed("不能给自己发送站内信");
        }
        //3.效验接受者是否存在
        User receiveUser = userService.selectById(receiveUserId);
        if (receiveUser==null || receiveUser.getDeleteState()==1){
            // 返回接受者状态不正常
            return AppResult.failed("接受者状态异常");
        }
        //4.封装对象
        Message message = new Message();
        message.setPostUserId(user.getId());
        message.setReceiveUserId(receiveUserId);
        message.setContent(content);
        //5.调用service
        messageService.create(message);
        //6.返回结果
        return  AppResult.success("发送成功");
    }
//    获取数量
    @RequestMapping("/getUnreadCount")
    public  AppResult<Integer> getUnreadCount(HttpServletRequest request){

        //1.获取当前登录的用户信息
        HttpSession session= request.getSession(false);
        User user =(User) session.getAttribute(AppConfig.USER_SESSION);
        //2.调用service
        Integer count =messageService.selectUnreadCount(user.getId());// 当前登录用户的id就是接收者的id
        //3.返回结果
        return  AppResult.success(count);
    }

//    获取站内信
    @RequestMapping("/getAll")
    public  AppResult<List<Message>> getAll(HttpServletRequest request){

        //1.获取当前登录的用户信息
        HttpSession session= request.getSession(false);
        User user =(User) session.getAttribute(AppConfig.USER_SESSION);

        //2.调用service
        List<Message> messages =messageService.selectByReceiveUserId(user.getId());
        //3.返回结果
        return AppResult.success(messages);
    }

//    标记已读
    @RequestMapping("/markRead")
    public  AppResult markRead(HttpServletRequest request,
                               @RequestParam(value = "id")@NonNull Long id){

        //1.根据id查询站内信
        Message message = messageService.selectById(id);
        //2.站内信是否存在
        if (message==null || message.getDeleteState()==1){
            //返回错误信息
            return  AppResult.failed(ResultCode.FAILED_MESSAGE_NOT_EXITS);
        }
        //3.站内信是不是自己的
        HttpSession session =request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if (user.getId()!=message.getReceiveUserId()){
            //返回错误信息
            return AppResult.failed(ResultCode.FAILED_MESSAGE_NOT_EXITS);
        }
        // 调用service
        messageService.updateStateById(id,(byte) 1);
        //返回结果
        return AppResult.success();
    }

//    回复站内信
    @RequestMapping("/reply")
    public AppResult reply(HttpServletRequest request,
                           @RequestParam(value = "repliedId")@NonNull Long repliedId,
                           @RequestParam(value = "content")@NonNull String content){

//       效验当前登录用户的状态
        HttpSession session=request.getSession(false);
        User user=(User) session.getAttribute(AppConfig.USER_SESSION);
        if (user.getState()==1){
//            返回错误描述
            return  AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
//      效验要回复的站内信状态
        Message existsMessage=messageService.selectById(repliedId);
        if (existsMessage==null ||existsMessage.getDeleteState()==1){
            //返回错误描述
            return  AppResult.failed(ResultCode.FAILED_MESSAGE_NOT_EXITS);
        }
//        不能给自己回复
        if (user.getId() == existsMessage.getPostUserId()){
//            返回错误描述
            return AppResult.failed("不能给自己回复");
        }
//        构造对象
        Message message=new Message();
        message.setPostUserId(user.getId());
        message.setReceiveUserId(existsMessage.getReceiveUserId());
        message.setContent(content);
//        调用service
        messageService.reply(repliedId,message);
//        返回结果
        return AppResult.success();
    }
}
